﻿﻿@using FCZLM.Core.Models
@{
    Layout = null;
    var list = ViewBag._newsCategoryRepository as List<NewCategorys>;
}
<!DOCTYPE html>
<html>
<head>

    <meta name="viewport" content="width=device-width" />

    <title>Add</title>

    <link href="/layui/css/layui.css" rel="stylesheet" />

    

</head>
<body>
    <div>

        <div class="layui-card">

            <div class="layui-card-header">添加新闻</div>

            <div class="layui-card-body">

                <form class="layui-form">

                    <!-- 使用Ajax需要把它删除action="/admin/news/add" method="post"-->

                    <div class="layui-form-item">

                        <label class="layui-form-label">新闻标题</label>

                        <div class="layui-input-block">

                            <input type="text" name="Title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">

                        </div>

                    </div>

                    <div class="layui-form-item">

                        <label class="layui-form-label">新闻副标题</label>

                        <div class="layui-input-block">

                            <input type="text" name="SubTitle" required lay-verify="required" placeholder="请输入新闻副标题" autocomplete="off" class="layui-input">

                        </div>

                    </div>

                   

                    <div class="layui-form-item layui-form-text">

                        <label class="layui-form-label">新闻内容</label>

                        <div class="layui-input-block">

                            <textarea class="layui-textarea" id="content" placeholder="请输入内容" style="display: none" name="Content" autocomplete="off"></textarea>

                        </div>

                    </div>

                    <div class="layui-form-item">

                        <label class="layui-form-label">点击量</label>

                        <div class="layui-input-block">

                            <input type="text" name="Clicks" required lay-verify="required" placeholder="请输入点击量" autocomplete="off" class="layui-input">

                        </div>

                    </div>

                    <div class="layui-form-item">

                        <label class="layui-form-label">新闻来源</label>

                        <div class="layui-input-block">

                            <input type="text" name="Source" required lay-verify="required" placeholder="请输入新闻来源" autocomplete="off" class="layui-input">

                        </div>

                    </div>

                    <div class="layui-form-item">

                        <label class="layui-form-label">新闻分类</label>

                        <div class="layui-input-block">

                            <select name="NewCategoryId" lay-verify="required">

                                <option value=""></option>

                                @foreach (var item in list)

                                {

                                    <option value="@item.Id">@item.Name</option>

                                }

                            </select>

                        </div>

                    </div>
                     <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="radio" name="Status" value="0" title="草稿">
                            <input type="radio" name="Status" value="1" title="发布" >
                        </div>
                    </div>
                    <div class="layui-form-item">

                        <div class="layui-input-block">

                            <button class="layui-btn" lay-submit lay-filter="addForm" data-type="text" id="fileNotifySubmit">添加</button><!--使用Ajax把formDemo改为addForm-->

                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>

                        </div>

                    </div>

                </form>

            </div>

        </div>

    </div>
    <script src="/layui/layui.js"></script>
    <script>
        layui.use(['layedit', 'form'], function () {
            var layedit = layui.layedit
                , $ = layui.jquery
                , form = layui.form;
            //设置图片上传
            layedit.set({
                uploadImage: {
                    url: '/fproducturls/index' //图片上传方法的接口url
                    , type: 'post' //默认post
                }
            });
            //构建富文本编辑器，构建方法一定要写在set方法下边
            var index = layedit.build('content');

            //校验表单的方法，并将富文本编辑器中的内容同步到文本域中
            form.verify({
                content: function (value) {
                    return layedit.sync(index); //同步语句
                }
            });

            //保存表单内容提交的方法
            $('#fileNotifySubmit').on('click', function () {
                var active = {
                    //获取纯文本内容： 111
                    //如果要获取样式，将text替换成 content: <p>111</p>
                    //如果要获取选中的文字，将text替换成selected：111
                    text: function () {
                        //获取富文本编辑器值的语句并赋值到文本域中
                        document.getElementById("content").innerText = layedit
                            .getContent(index);
                    }
                };

                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
                var form = new FormData(document.getElementById("formFile"));

                //保存表单内容的请求，此处为文件通知对象
                $.ajax({
                    url: "/fnews/add",
                    type: "post",
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (result) {
                        if (result.status === true) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.window.location.reload()
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 1000 });
                        }
                    }
                });
            });
        });
        //重新加载的方法
        layui.use(['table', 'laydate', 'element'], function () {
            var table = layui.table, form = layui.form, $ = layui.jquery;
        });
      
        //引入layui组件
        //layui.use("form", function () { });//使用Ajax方式需要把这行删除
        //使用Ajax方式
        layui.use(["form", 'layer'], function () {

            var form = layui.form;

            var layer = layui.layer;

            var $ = layui.$;//引入layui组件

            //监听提交

            form.on('submit(addForm)', function (data) {
                $.post("/fnews/Add", data.field, function (res) {

                    if (res.isSuccess) {
                        layer.msg(res.msg, function () {
                            parent.window.location.href = "/fnews/index"//parent加上它是父页面做跳转
                        });
                    } else {
                        layer.msg(res.msg)
                    }
                });
                return false;
            });
        });
    </script>

</body>

</html>
